﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace ScannerDemo
{
    public partial class select : PhoneApplicationPage
    {
        public select()
        {
            InitializeComponent();
            check_scan.IsChecked = check_generate.IsChecked = check_all.IsChecked = check_http.IsChecked = check_vcard.IsChecked = check_txt.IsChecked = true;
            
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string tempString = "";


            if ((NavigationContext.QueryString.Count > 0))
            {
                string check_scan1 = "";
                string check_generate1 = "";
                string check_all1 = "";
                string check_http1 = "";
                string check_vcard1 = "";
                string check_txt1 = "";
                NavigationContext.QueryString.TryGetValue("check_scan", out check_scan1);
                NavigationContext.QueryString.TryGetValue("check_generate", out check_generate1);
                NavigationContext.QueryString.TryGetValue("check_all", out check_all1);
                NavigationContext.QueryString.TryGetValue("check_http", out check_http1);
                NavigationContext.QueryString.TryGetValue("check_vcard", out check_vcard1);
                NavigationContext.QueryString.TryGetValue("check_txt", out check_txt1);
                if (check_scan1 == "True")
                {
                    check_scan.IsChecked = true;
                }
                else
                {
                    check_scan.IsChecked = false;
                }
                if (check_generate1 == "True")
                {
                    check_generate.IsChecked = true;
                }
                else
                {
                    check_generate.IsChecked = false;
                }
                if (check_all1 == "True")
                {
                    check_all.IsChecked = true;
                }
                else
                {
                    check_all.IsChecked = false;
                }
                if (check_http1 == "True")
                {
                    check_http.IsChecked = true;
                }
                else
                {
                    check_http.IsChecked = false;
                }
                if (check_vcard1 == "True")
                {
                    check_vcard.IsChecked = true;
                }
                else
                {
                    check_vcard.IsChecked = false;
                }
                if (check_txt1 == "True")
                {
                    check_txt.IsChecked = true;
                }
                else
                {
                    check_txt.IsChecked = false;
                }
            }
            
        }
        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            if (check_generate.IsChecked==false && check_scan.IsChecked==false)
            {
                MessageBox.Show("请至少选择一项条件！扫描或者生成！");
            }
            else if (check_http.IsChecked == false && check_vcard.IsChecked == false && check_txt.IsChecked == false)
            {
                MessageBox.Show("请至少选择一项条件！网址,名片或者文本！");
            }
            else
            {
                string uriString = @"/ScannerDemo;component/HomePagePivot.xaml?pivotIndex=1&check_scan=" + check_scan.IsChecked + "&check_generate=" + check_generate.IsChecked
                    + "&check_all=" + check_all.IsChecked + "&check_http=" + check_http.IsChecked + "&check_vcard=" + check_vcard.IsChecked + "&check_txt=" + check_txt.IsChecked;
                NavigationService.Navigate(new Uri(uriString, UriKind.RelativeOrAbsolute));
            }
        }

        private void check_all_Checked_1(object sender, RoutedEventArgs e)
        {/*
            check_http.IsChecked = true;
            check_vcard.IsChecked = true;
            check_txt.IsChecked = true;
          */
        }

        private void check_all_Unchecked_1(object sender, RoutedEventArgs e)
        {
            /*
            check_http.IsChecked = false;
            check_vcard.IsChecked = false;
            check_txt.IsChecked = false;
             */ 
        }

        private void check_scan_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void check_http_Checked_1(object sender, RoutedEventArgs e)
        {
            autoCheckAll();
        }

        private void check_vcard_Checked_1(object sender, RoutedEventArgs e)
        {
            autoCheckAll();
        }

        private void check_txt_Checked_1(object sender, RoutedEventArgs e)
        {
            autoCheckAll();
        }
        private void autoCheckAll()
        {
            if ((check_http.IsChecked==true) && (check_vcard.IsChecked==true) && (check_txt.IsChecked==true))
                check_all.IsChecked = true;

        }
        private void autoUncheckAll()
        {

            if ((check_http.IsChecked == false) || (check_vcard.IsChecked == false) || (check_txt.IsChecked == false))
                check_all.IsChecked = false;
        }



        private void check_http_Unchecked_1(object sender, RoutedEventArgs e)
        {
            autoUncheckAll();
        }

        private void check_vcard_Unchecked_1(object sender, RoutedEventArgs e)
        {
            autoUncheckAll();
        }

        private void check_txt_Unchecked_1(object sender, RoutedEventArgs e)
        {
            autoUncheckAll();
        }

        private void check_all_Click_1(object sender, RoutedEventArgs e)
        {
            if (check_all.IsChecked == true)
            {
                check_all.IsChecked = true;
                check_http.IsChecked = true;
                check_vcard.IsChecked = true;
                check_txt.IsChecked = true;

            }
            else
            {
                check_all.IsChecked = false;
                check_http.IsChecked = false;
                check_vcard.IsChecked = false;
                check_txt.IsChecked = false;
            }
        }
    }
}